package cn.zhangjie.product.controller;

import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.product.domain.Product;
import cn.zhangjie.product.query.ProductQuery;
import cn.zhangjie.product.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2022-08-11 23:27:38
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService ProductService;


    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onsale(@RequestBody List<Long> ids){

        try {
            ProductService.onsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offsale(@RequestBody List<Long> ids){

        try {
            ProductService.offsale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("批量删除失败");
        }
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<Product> queryPage(@RequestBody ProductQuery query) {
	try {
		return ProductService.queryPage(query);
	} catch (Exception e) {
		e.printStackTrace();
        return new PageList<>();
	    }
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询")
    public Product findOne(@PathVariable("id") Long id) {
	    return ProductService.findById(id);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping()
    @ApiOperation(value = "查询所有")
    public List<Product> findAll() {
	    return ProductService.findAll();
    }

    /**
     * 新增or修改数据
     *
     * @param product 实体
     * @return 新增or修改结果
     */
    @PutMapping
    @ApiOperation(value = "添加or修改")
    public JsonResult addOrUpdate(@RequestBody Product product) {
        try {
		if(product.getId() == null){
			//添加操作
            ProductService.add(product);
		} else {
			//修改操作
            ProductService.update(product);
		}
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("添加or修改失败");
	    }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            ProductService.del(id);
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("网络异常,稍后重试");
	    }
    }

    /**
     * 批量删除
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            ProductService.pactchdel(ids);
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("网络异常,稍后重试");
	    }
    }


}

